// service/serveList/serveList.js
const app = getApp()
import http from "../../utils/http"
import util from "../../utils/util"
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list:[],
    finish: false,
    loading: false,
    page: 1,
    type: 1,
    safeBottom: `padding-bottom:100px`,
    goods_id: '',
    name: ''
  },
  select(e){
    let id = e.currentTarget.dataset.item.id;
    let name = e.currentTarget.dataset.item.name;
    this.setData({
      goods_id: id,
      name: name
    })
  },
  detail(e){
    let id = e.currentTarget.dataset.id;
    util.skip('/service/servedetail/servedetail?id='+id)
  },
  change(){
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.emit('selectSelfSevel',{goods_id: this.data.goods_id,name: this.data.name});
    util.back();
  },
  reload(){
    this.setData({
      list: [],
      page: 1,
      finish: false
    })
    this.getList()
  },
  more(){
    this.setData({
      page: ++this.data.page
    })
    this.getList()
  },
  getList(){
    if(this.data.finish){
      return 
    }
    if(this.data.loading){
      return 
    }
    this.setData({
      loading: true
    })
    http.get('shop/shopgoods', {
      page: this.data.page,
      shop_state: 1
    }).then(res => {
      if(res.data.list.length === 0){
        this.setData({
          finish: true
        })
      }
      let arr = this.data.list.concat(res.data.list)
      this.setData({
        list: arr
      })
      
    }).finally(res => {
      this.setData({
        loading: false
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const high = app.globalData.safeBottom
    this.setData({
      safeBottom: `padding-bottom:${high}px`,
      goods_id: options.goods_id || '',
      name: options.name || '',
    })
    this.reload()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})